// pages/register/register.js
Page({
  data: {
    username: '',
    password: '',
    confirmPassword: '',
    phone: '',
    usernameTip: '',
    passwordTip: '',
    confirmPasswordTip: '',
    phoneTip: '',
    errorMsg: '',
    isLoading: false
  },

  onLoad(options) {
    // 页面加载时执行的逻辑
  },

  onUsernameInput(e) {
    const username = e.detail.value;
    this.setData({
      username,
      usernameTip: username ? '' : '请输入用户名',
      errorMsg: ''
    });
  },

  onPasswordInput(e) {
    const password = e.detail.value;
    let tip = '';
    
    if (password && password.length < 8) {
      tip = '密码长度至少为8位';
    }
    
    this.setData({
      password,
      passwordTip: tip,
      errorMsg: ''
    });
    
    // 如果已经输入了确认密码，检查两次密码是否一致
    if (this.data.confirmPassword) {
      this.checkPasswordConsistency();
    }
  },

  onConfirmPasswordInput(e) {
    const confirmPassword = e.detail.value;
    this.setData({
      confirmPassword,
      errorMsg: ''
    });
    
    if (confirmPassword) {
      this.checkPasswordConsistency();
    } else {
      this.setData({
        confirmPasswordTip: ''
      });
    }
  },

  checkPasswordConsistency() {
    const { password, confirmPassword } = this.data;
    
    if (password !== confirmPassword) {
      this.setData({
        confirmPasswordTip: '两次输入的密码不一致'
      });
    } else {
      this.setData({
        confirmPasswordTip: ''
      });
    }
  },

  onPhoneInput(e) {
    const phone = e.detail.value;
    let tip = '';
    
    if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
      tip = '请输入正确的手机号码';
    }
    
    this.setData({
      phone,
      phoneTip: tip,
      errorMsg: ''
    });
  },

  // 处理注册请求
  handleRegister() {
    const { username, password, confirmPassword, phone } = this.data;
    
    // 表单验证
    if (!username.trim()) {
      this.setData({ errorMsg: '请输入用户名' });
      return;
    }
    
    if (!password) {
      this.setData({ errorMsg: '请设置密码' });
      return;
    }
    
    if (password.length < 8) {
      this.setData({ errorMsg: '密码长度至少为8位' });
      return;
    }
    
    if (password !== confirmPassword) {
      this.setData({ errorMsg: '两次输入的密码不一致' });
      return;
    }
    
    if (!phone) {
      this.setData({ errorMsg: '请输入手机号码' });
      return;
    }
    
    if (!/^1[3-9]\d{9}$/.test(phone)) {
      this.setData({ errorMsg: '请输入正确的手机号码' });
      return;
    }
    
    // 显示加载状态
    this.setData({ isLoading: true });
    
    // 发送注册请求
    wx.request({
      url: 'https://smartsound.top/api/v1/register',
      method: 'POST',
      data: {
        username: username,
        password: password,
        phonenumber: parseInt(phone)
      },
      success: (res) => {
        if (res.data.code === 0) {
          // 注册成功
          wx.showToast({
            title: '注册成功',
            icon: 'success',
            duration: 2000,
            success: () => {
              // 延迟跳转到登录页面
              setTimeout(() => {
                wx.navigateBack();
              }, 2000);
            }
          });
        } else {
          // 注册失败，显示错误信息
          let errorMessage = '注册失败';
          if (res.data.message) {
            errorMessage = res.data.message;
          }
          this.setData({ 
            errorMsg: errorMessage,
            isLoading: false
          });
        }
      },
      fail: (err) => {
        console.error('注册请求失败', err);
        this.setData({ 
          errorMsg: '网络错误，请稍后重试',
          isLoading: false
        });
      }
    });
  },

  // 跳转到登录页面
  navigateToLogin() {
    wx.navigateBack();
  }
});